package com.sesame.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.sesame.defin.AbstractController;
import com.sesame.defin.Response;
import com.sesame.entity.User;
import com.sesame.feign.FeignServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
public class TestController  extends AbstractController {

    static final String serviceName = "nacos-discovery-server";

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    FeignServiceTest feignServiceTest;

    /**
     * http://localhost:8092/index?name=1&pwd=aaa
     */
    @RequestMapping("/index")
    public String index(String name,String pwd) {
        String url = MessageFormat.format("http://{0}/test/index?name={1}&pwd={2}", serviceName, name,pwd);
        return restTemplate.getForObject(url, String.class);
    }

    /**
     * http://localhost:8085/getData?name=1&pwd=bbb
     */
    @RequestMapping("/getData")
    public ApiResult<User> getData(String name, String pwd) {
        Response<User> json = feignServiceTest.test(name, pwd);
        return json;
    }

    @NacosInjected
    NamingService namingService;

    /**
    http://localhost:8095/getServiceList?serviceName=nacos-discovery-server
     */
    @RequestMapping("/getServiceList")
    public ApiResult getServiceList(String serviceName) {
        List<Instance> list = new ArrayList<>();
        try {
            list = namingService.getAllInstances("serviceName");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success(list);
    }

}
